package com.javaweb.controller;

import com.javaweb.controller.common.ResponseResult;
import com.javaweb.model.SimpleDto;
import com.javaweb.model.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/10/23 9:31
 */

/**
 *
 *  Controller:
 *   1. HttpServletRequest:
 *   2. 参数绑定
 *   3. Response:
 *       (1) model
 *       (2) (***) Response Object (ResponseEntity) in json string
 *   4. 全局异常处理
 *   5. view resolver: Thymeleaf
 *   6. 拦截器
 *   7.Filter
 *   8.文件上传
 *
 *
 *
 *
 *
 */
@Controller
@Slf4j
public class HelloController {

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello() {
        return "hello";
    }
//@RequestMapping(value = "/name/hello", method = RequestMethod.GET)
    @GetMapping("/name/hello")
    @ResponseBody
    public String hello(@RequestParam String name) {
        return "hello " + name ;
    }

    @GetMapping("/req/hello")
    @ResponseBody
    public String helloReq(HttpServletRequest req) {

        StringBuilder stringBuilder = new StringBuilder() ;
        stringBuilder.append("Request Method: " + req.getMethod() + "\n")
                .append("\n Request URL: " + req.getRequestURL() + "\n")
                .append("\n Request User_Agent: " + req.getHeader("User-Agent") )
                .append("\n Request QueryString: " +req.getQueryString()) ;
        log.info(stringBuilder.toString());
        return "hello http-servlet-request: " + stringBuilder.toString() ;
    }

    @PostMapping("/req/model")
    @ResponseBody
    public ResponseResult<UserVo> processModel(@ModelAttribute UserVo user)  {
        log.info("recevied: " + user) ;
        return ResponseResult.<UserVo>ok(user) ;


    }

    @PostMapping("/req/json/model")
    @ResponseBody
    public ResponseResult<UserVo> processModelInJson(@RequestBody UserVo user)  {
        log.info("received user: " + user) ;
        return ResponseResult.<UserVo>ok(user) ;


    }

    @PostMapping("/req/json/dto")
    @ResponseBody
    public ResponseResult<SimpleDto> processDtoInJson(@RequestBody SimpleDto simpleDto) {
        log.info("received simpleDto: " + simpleDto) ;
        return ResponseResult.<SimpleDto>ok(simpleDto) ;
    }




}
